package com.backend.controller;

import com.backend.entity.User;
import com.backend.repository.UserRepository;
import com.backend.tool.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserCtrl {
    @Autowired
    UserRepository userRepository;

    @GetMapping("/get")
    public Result getById(@Param("id") Integer id){
        Optional<User> optional = userRepository.findById(id);
        if(optional.isPresent()){
            return Result.success(optional.get().toFront());
        }
        return Result.error(201,"用户"+id+"不存在");
    }

    @GetMapping("/checktoken")
    public Result checkToken(@Param("token") String token){
        int code = JwtUtils.verifyToken(token);
        if(code == 0){
            return Result.success();
        }else{
            return Result.error(204,"请登录");
        }
    }
}
